var app = getApp();
var submitapi = require('../../api/note.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    robotdata:{},

    notedata:[],
    textarealength: 0,
    score: 5,
    textdata: "",
    type:"robot",
    show:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData(
      {id: options.id}
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    })
    const db = wx.cloud.database()
    // 查询当前用户所有的 counters
    try {
      db.collection('wrobot').where({
        id: that.data.id
      }).get({
        success: res => {
          var detailobj = res.data[0];
          var dataobj = JSON.parse(detailobj.data)
          detailobj.data = dataobj
          this.setData({
            robotdata: detailobj
          })
          wx.hideToast();
        },
        fail: err => {
          wx.showToast({
            icon: 'none',
            title: '查询记录失败'
          })
          wx.hideToast();
        }
      })
    } catch (e) {
      wx.showToast({
        title: '读取数据失败，请检查网络',
        icon: 'none',
        duration: 10000
      })
    }
    this.setData({
      show: app.globalData.state
    })
   
    submitapi.getnote(this);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  //发表留言块
  sliderchange: function (e) {
    this.setData({
      score: e.detail.value
    })
  },
  textareachange: function (e) {
    this.setData({
      textarealength: e.detail.cursor
    })
    this.setData({
      textdata: e.detail.value
    })
  },
  showToast: function (title, icon){
    wx.showToast({
      title: title,
      icon: icon,
      duration: 3000
    })
  },
  allmsg:function(){
    wx.navigateTo({
      router: 'robot',
      url: '/pages/Wmsg/Wmsg' + '?id=' + this.data.id
    })
  },
  submit: function (uinfo) {
    var that=this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          submitapi.submitNoteApi(that, uinfo);
        } else {
          that.showToast('评论功能需要获取头像和昵称。', 'none');
          wx.openSetting({
            success(res) {
              
              // res.authSetting = {
              //   "scope.userInfo": true
              // }
            }
          })
        }
      }
    })
  }
})